<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 楼栋
 *
 * @package app\api\controller
 */
class Building extends Api
{
    protected $noNeedLogin = ['cxSelect'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    public function profile()
    {
        $id = $this->request->post('id', 0);

        $data = \app\common\model\Building::alias('building')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id')
            ->where('building.id', $id)
            ->field('building.*,garden.number garden_number,garden.school_id')
            ->find();

        $this->success('', $data);
    }

    /**
     * @ApiTitle    (楼栋列表)
     * @ApiAuthor   (Zhenzh)
     * @ApiDate     (2020/2/7)
     * @ApiSummary  (描述)
     * @ApiBody     (Body)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/building/cxSelect)
     * @ApiHeaders  (name="token", type="string", required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, description="状态码 1正常 0错误", sample="1")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", description="扩展数据返回", sample="")
     * @ApiReturn ()
     */
    public function cxSelect()
    {
        $garden_id = $this->request->post('garden_id', 0);
        $type = $this->request->post('type/a');

        $list = \app\common\model\Building::where('garden_id', $garden_id)
            ->where(function ($query) use ($type) {
                if ($type) {
                    $query->whereIn('type', $type);
                }
            })
            ->field('id,number,name,floor')
            ->select();

        if ($list) {
            $list = collection($list)->toArray();
            foreach ($list as $key => $value) {
                $list[$key]['floors'] = [];
                for ($i = 1; $i <= $value['floor']; $i++) {
                    $list[$key]['floors'][] = [
                        'id' => $i,
                        'name' => $i . '楼'
                    ];
                }
            }
        }

        $this->success('', $list);
    }

    /**
     * 寝室信息
     *
     * @User ZhenZiHan
     * @Date 2020/2/17
     *
     */
    public function dorm()
    {
        $school_id = $this->request->post('school_id', 0);
        $garden_number = $this->request->post('garden_number', '');
        $building_number = $this->request->post('building_number', '');

        $result = \app\common\model\Building::alias('building')
            ->join('__GARDEN__ garden', 'garden.id = building.garden_id')
            ->join('__SCHOOL__ school', 'school.id = garden.school_id')
            ->where('school.id', $school_id)
            ->where('garden.number', $garden_number)
            ->where('building.number', $building_number)
            ->field('school.id school_id,school.name school_name,garden.id garden_id,garden.number garden_number,garden.name garden_name,building.id building_id,building.number building_number,building.name building_name')
            ->find();

        $this->success('', $result);
    }

}
